import re

# 创建一个空的院系信息列表
departments = []


def add():
    """添加院系信息"""
    while True:
        department_name = input("请输入院系名称: ")
        if not re.match("^[\u4e00-\u9fa5]+$", department_name):
            print("院系名称必须是汉字，请重新输入！")
        else:
            break

    while True:
        class_name = input("请输入班级名称（例如班级为5班则格式为Z220105）: ")
        if not re.match("^Z22010[1-9]$", class_name):
            print("班级名称必须符合Z22010X的格式，X为对应的班级，请重新输入！")
        else:
            break

    class_size = int(input("请输入班级人数: "))

    while True:
        teacher_name = input("请输入班导姓名: ")
        if not re.match("^[\u4e00-\u9fa5]+$", teacher_name):
            print("班导姓名必须是汉字，请重新输入！")
        else:
            break

    while True:
        teacher_contact = input("请输入班导联系电话（11位中国手机号码）: ")
        if not re.match("^(13[0-9]|14[56789]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$", teacher_contact):
            print("班导联系电话必须是正确的11位中国手机号码，请重新输入！")
        else:
            break

    male_count = int(input("请输入男生人数: "))
    female_count = class_size - male_count
    male_ratio = male_count / class_size
    female_ratio = female_count / class_size
    single_rate = female_count / class_size * 100

    department = {
        "院系名称": department_name,
        "班级名称": class_name,
        "班级人数": class_size,
        "班导姓名": teacher_name,
        "班导联系电话": teacher_contact,
        "男生人数": male_count,
        "女生人数": female_count,
        "男女人数比例": f"{male_ratio:.2f}:{female_ratio:.2f}",
        "脱单率": f"{single_rate:.2f}%"
    }

    departments.append(department)

    if single_rate < 30:
        print("院系信息添加成功")
        print("班级男女比例失衡，建议重新调整该班级的男女比例")
    else:
        print("院系信息添加成功")


def delete():
    """删除院系信息"""
    department_name = input("请输入要删除的院系名称: ")
    deleted = False

    for department in departments:
        if department["院系名称"] == department_name:
            departments.remove(department)
            deleted = True
            break

    if deleted:
        print("院系信息删除成功")
    else:
        print("未找到该院系信息")


def update():
    """修改院系信息"""
    department_name = input("请输入要修改的院系名称: ")
    updated = False

    for department in departments:
        if department["院系名称"] == department_name:
            new_department_name = input("请输入新的院系名称: ")
            if not re.match("^[\u4e00-\u9fa5]+$", new_department_name):
                print("院系名称必须是汉字，请重新输入！")
                return  # Stop further execution if the department name is invalid

            while True:
                class_name = input("请输入新的班级名称（例如Z220105）: ")
                if not re.match("^Z22010[1-9]$", class_name):
                    print("班级名称必须符合Z22010X的格式，X为对应的班级，请重新输入！")
                else:
                    break

            class_size = int(input("请输入新的班级人数: "))

            while True:
                teacher_name = input("请输入班导姓名: ")
                if not re.match("^[\u4e00-\u9fa5]+$", teacher_name):
                    print("班导姓名必须是汉字，请重新输入！")
                else:
                    break

            while True:
                teacher_contact = input("请输入班导联系电话（11位中国手机号码）: ")
                if not re.match("^(13[0-9]|14[56789]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$", teacher_contact):
                    print("班导联系电话必须是正确的11位中国手机号码，请重新输入！")
                else:
                    break

            department["院系名称"] = new_department_name
            department["班级名称"] = class_name
            department["班级人数"] = class_size
            department["班导姓名"] = teacher_name
            department["班导联系电话"] = teacher_contact

            male_count = int(input("请输入男生人数: "))
            female_count = class_size - male_count
            male_ratio = male_count / class_size
            female_ratio = female_count / class_size
            single_rate = female_count / class_size * 100

            department["男生人数"] = male_count
            department["女生人数"] = female_count
            department["男女人数比例"] = f"{male_ratio:.2f}:{female_ratio:.2f}"
            department["脱单率"] = f"{single_rate:.2f}%"

            updated = True
            break

    if updated:
        print("院系信息修改成功")
    else:
        print("未找到该院系信息")






def select():
    """查询院系信息"""
    department_name = input("请输入要查询的院系名称: ")
    found = False

    for department in departments:
        if department["院系名称"] == department_name:
            print("院系名称:", department["院系名称"])
            print("班级名称:", department["班级名称"])
            print("班级人数:", department["班级人数"])
            print("班导姓名:", department["班导姓名"])
            print("班导联系电话:", department["班导联系电话"])
            print("男生人数:", department["男生人数"])
            print("女生人数:", department["女生人数"])
            print("男女人数比例:", department["男女人数比例"])
            print("脱单率:", department["脱单率"])
            found = True
            break

    if not found:
        print("未找到该院系信息")




def main():
    """主函数，控制程序运行流程"""
    while True:
        print("欢迎使用学生管理系统 - 院系模块")
        print("1. 添加院系信息")
        print("2. 删除院系信息")
        print("3. 修改院系信息")
        print("4. 查询院系信息")
        print("5. 退出院系模块")
        choice = input("请输入功能序号: ")
        if choice == "1":
            add()
        elif choice == "2":
            delete()
        elif choice == "3":
            update()
        elif choice == "4":
            select()
        elif choice == "5":
            print("感谢使用学生管理系统 - 院系模块，再见！")
            break
        else:
            print("无效的选择，请重新输入！")


# 运行主函数
main()
